//
//  ChangeLoginPwdVC.m
//  Cunli
//
//  Created by lilubin on 15/6/29.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "ChangeLoginPwdVC.h"
#import "NSString+MD5.h"
#import "MyDataService.h"
#import "MBProgressHUD.h"
#import "Config.h"


@interface ChangeLoginPwdVC ()
@property (weak, nonatomic) IBOutlet UILabel *oldPwdLabel;            //原密码label
@property (weak, nonatomic) IBOutlet UILabel *myNewPwdLabel; //新密码label
@property (weak, nonatomic) IBOutlet UILabel *myNewPwdLabel2; //确认新密码label
@property (weak, nonatomic) IBOutlet UITextField *oldPwdTextfield; //原密码输入框
@property (weak, nonatomic) IBOutlet UITextField *myNewPwdTextfield; //新密码输入框
@property (weak, nonatomic) IBOutlet UITextField *myNewPwdTextfield2; //确认新密码输入框

@end

@implementation ChangeLoginPwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录密码修改";
    [self setAllUIKitAttributes];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)setAllUIKitAttributes
{
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 65, 30);
    [rightBtn setTitle:@"完成" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(commitAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    flexSpacer.width = flexSpacer.width - 20;
    
    UIColor *rightLabelColor = [UIColor colorWithHexString:@"0x555555"];
    [_oldPwdLabel setTextColor:rightLabelColor];
    [_myNewPwdLabel setTextColor:rightLabelColor];
    [_myNewPwdLabel2 setTextColor:rightLabelColor];
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:flexSpacer, rightBarItem, nil]];
}

- (void)commitAction:(UIButton *)button
{
    [self.view endEditing:YES];
    //判断输入框的内容是否合法
    if (_oldPwdTextfield.text.length < 6 || _myNewPwdTextfield.text.length < 6 || _myNewPwdTextfield2.text.length < 6) {
        [self showHUDWithWaring:@"密码需要大于6位"];
        return;
    }
    if (![_myNewPwdTextfield.text isEqualToString:_myNewPwdTextfield2.text]) {
        [self showHUDWithWaring:@"新密码输入不一致"];
        return;
    }
    
    NSMutableDictionary *CheckParams = [NSMutableDictionary dictionaryWithCapacity:5];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:_oldPwdTextfield.text forKey:@"pwd"];
    [CheckParams setObject:_myNewPwdTextfield.text forKey:@"new_pwd"];
    [CheckParams setObject:_myNewPwdTextfield2.text forKey:@"again_pwd"];
    
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];

    [MyDataService requestURL:@"edit_password.php" httpMethod:@"POST" params:CheckParams fileDatas:nil complection:^(id result) {
//        NSLog(@"Result:%@", result);
        MBProgressHUD *hud = [Config createHUD];
        hud.mode = MBProgressHUDModeCustomView;
        NSString *response = result[@"response"];
        if ([response isEqualToString:@"edit_password"]) {
            hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"done"]];
            hud.labelText = @"修改成功";
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            NSDictionary *errorDic = [result objectForKey:@"error"];
            NSString *errorStr = [errorDic objectForKey:@"text"];
            hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
            hud.labelText = errorStr;
            
//            return;
        }
        [hud hide:YES afterDelay:1.2];

    } failureAction:^{
        MBProgressHUD *hud = [Config createHUD];
        hud.mode = MBProgressHUDModeCustomView;
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
        hud.labelText = @"修改失败";
        [hud hide:YES afterDelay:1.2];
    }];
    
}

- (void)showHUDWithWaring:(NSString *)hudStr
{
    MBProgressHUD *hud = [Config createHUD];
    hud.mode = MBProgressHUDModeCustomView;
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
    hud.labelText = hudStr;
    [hud hide:YES afterDelay:1.2];
}


@end
